using PK.Broadcaster;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelEngine;

namespace VoxelGame.Character
{
	public class CharacterToolPickaxe : CharacterTool, IMessageSubscriber<BlockChangedMessage>
	{
		private enum CharacterToolPickaxeState
		{
			FullBlock,
			HalfBlock,
			AsteriskBlock
		}

		[SerializeField]
		private MeshRenderer _fullBlockMeshRenderer;

		[SerializeField]
		private MeshRenderer _halfBlockMeshRenderer;

		[SerializeField]
		private MeshRenderer _extrudedBlockMeshRenderer;

		[SerializeField]
		private Material _topMaterial;

		[SerializeField]
		private Material _sideMaterial;

		[SerializeField]
		private Material _bottomMaterial;

		private Texture WorldTexture => BaseWorldController.VoxelPlayerData.VoxelWorldData.OpaqueMaterial.mainTexture;

		protected override void Awake()
		{
			base.Awake();
			_topMaterial.mainTexture = WorldTexture;
			_sideMaterial.mainTexture = WorldTexture;
			_bottomMaterial.mainTexture = WorldTexture;
			MessageBroadcaster.Instance.Subscribe(this);
		}

		public override void Setup(CharacterSoundController soundController)
		{
			base.Setup(soundController);
			MessageBroadcaster.Instance.Subscribe(this);
		}

		private void OnDestroy()
		{
			MessageBroadcaster.Instance.Unsubscribe(this);
		}

		public override bool CreateBlock(VoxelEngine.World world, Block selectedBlock, BlockType blockType)
		{
			bool flag = selectedBlock.Create(blockType);
			if (flag)
			{
				Play();
				_soundController.PlayCreateBlock();
				world.RefreshChunkMesh(selectedBlock, null);
			}
			return flag;
		}

		public override IEnumerator DestroyBlock(VoxelEngine.World world, Block selectedBlock, Action<Dictionary<BlockType, int>> onBlocksDestroyed, byte blockLight = 0)
		{
			BlockType blockType = selectedBlock.Type;
			if (selectedBlock.Destroy(blockLight))
			{
				Play();
				world.RefreshChunkMesh(selectedBlock, null);
				_soundController.PlayDestroyBlock();
				Dictionary<BlockType, int> dictionary = new Dictionary<BlockType, int>();
				dictionary.Add(blockType, 1);
				Dictionary<BlockType, int> arg = dictionary;
				onBlocksDestroyed.Fire(arg);
			}
			yield return null;
		}

		public void OnMessageReceived(BlockChangedMessage message)
		{
			SetToolAccordingToBlockType(message.BlockTypeSelected);
		}

		private void SetToolAccordingToBlockType(BlockType blockType)
		{
			if (Block.IsAsteriskTranslucentBlock(blockType))
			{
				SetToolToState(CharacterToolPickaxeState.AsteriskBlock, blockType);
			}
			else if (Block.IsHalfBlock(blockType))
			{
				SetToolToState(CharacterToolPickaxeState.HalfBlock, blockType);
			}
			else
			{
				SetToolToState(CharacterToolPickaxeState.FullBlock, blockType);
			}
		}

		private void SetToolToState(CharacterToolPickaxeState state, BlockType blockType)
		{
			BlockBase blockType2 = BlockTypes.Instance.GetBlockType(blockType);
			_fullBlockMeshRenderer.gameObject.SetActive(state == CharacterToolPickaxeState.FullBlock);
			_halfBlockMeshRenderer.gameObject.SetActive(state == CharacterToolPickaxeState.HalfBlock);
			_extrudedBlockMeshRenderer.gameObject.SetActive(state == CharacterToolPickaxeState.AsteriskBlock);
			_topMaterial.mainTextureOffset = blockType2.TextureTopUV();
			_sideMaterial.mainTextureOffset = blockType2.TextureSideUV();
			_bottomMaterial.mainTextureOffset = blockType2.TextureBottomUV();
			_sideMaterial.color = ((state != CharacterToolPickaxeState.AsteriskBlock) ? Color.white : Color.clear);
		}
	}
}
